package com.huike.pmps.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huike.pmps.dao.RegionDao;
import com.huike.pmps.model.dto.common.PageInfo;
import com.huike.pmps.model.entity.Region;
import com.huike.pmps.model.vo.PageResult;
import com.huike.pmps.model.vo.Result;
import com.huike.pmps.service.RegionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 作者：谌贵斌(黑桃K)
 * 日期: 2888-08-08
 * 老师QQ: 272488352
 * 官方QQ交流群: 377748272
 * 刀客程序员官网：http://www.daoke360.com
 * 刀客程序员淘宝旗舰店：https://daoke360.taobao.com/
 */
@Service
public class RegionServiceImpl implements RegionService {
    @Autowired
    RegionDao regionDao;

    @Override
    public Result insertOne(Region region) {
        region.setCreateTime(new Date());
        if (region.getParentId() == null) {
            region.setParentId(0);
            List<Region> regions = selectByRegionName(region);
            if (regions.size() > 0) return Result.failure("战区名重复，请勿重复添加");
        } else {
            QueryWrapper<Region> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("province_id", region.getProvinceId());
            Region one = regionDao.selectOne(queryWrapper);
            if (one != null) return Result.failure("该省已经被添加的别的战区，请勿重复添加");
        }
        regionDao.insert(region);
        return Result.success();
    }

    @Override
    public Result updateById(Region region) {
        regionDao.updateById(region);
        if (region.getParentId() == 0)
            regionDao.updateDependent(region.getId(), region.getRegion());
        return Result.success();
    }

    @Override
    public Result deleteById(Integer id) {
        QueryWrapper<Region> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", id);
        if (regionDao.selectCount(queryWrapper) > 0) {
            return Result.failure("该战区下还有省级战区，请先删除省级战区");
        } else {
            regionDao.deleteById(id);
            return Result.success();
        }
    }

    /**
     * 按条件分页查询战区数据
     *
     * @param pageInfo
     * @param region
     * @param anotherName
     * @param charge
     * @return
     */
    @Override
    public Result selectByCondition(PageInfo pageInfo, String region, String anotherName, String charge) {
        QueryWrapper<Region> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "region", "another_name", "parent_id", "charge", "description", "create_time");
        queryWrapper.eq("parent_id", "0");
        if (StringUtils.isNotBlank(region)) {
            queryWrapper.eq("region", region);
        }
        if (StringUtils.isNotBlank(anotherName)) {
            queryWrapper.eq("another_name", anotherName);
        }
        if (StringUtils.isNotBlank(charge)) {
            queryWrapper.eq("charge", charge);
        }

        Page<Region> page = new Page<>(pageInfo.getPage(), pageInfo.getPageSize());
        Page<Region> pageList = regionDao.selectPage(page, queryWrapper);
        if (pageList.getSize() > 0) {
            PageResult pageResult = PageResult.builder().pageNumber(pageList.getCurrent()).pageSize(pageList.getSize())
                    .total(pageList.getTotal()).rows(pageList.getRecords()).build();
            return Result.success(pageResult);
        } else {
            return Result.success("没有符合条件的记录");
        }
    }

    @Override
    public Result selectAllRegion() {
        QueryWrapper<Region> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", "0");
        queryWrapper.select("id", "region", "another_name");
        return Result.success(regionDao.selectList(queryWrapper));
    }

    @Override
    public Result selectByParentId(Integer parentId) {
        QueryWrapper<Region> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "province_id", "province_name", "parent_id", "charge", "description", "create_time");
        queryWrapper.eq("parent_id", parentId);
        return Result.success(regionDao.selectList(queryWrapper));
    }

    @Override
    public Region selectOneByRegionName(String regionName) {
        QueryWrapper<Region> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "region", "another_name", "parent_id", "charge", "description", "create_time");
        queryWrapper.eq("parent_id", "0");
        queryWrapper.like("region", regionName).or().like("another_name", regionName);
        return regionDao.selectOne(queryWrapper);
    }

    @Override
    public Region selectParentRegionByProvinceName(String provinceName) {
        QueryWrapper<Region> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("province_name", provinceName);
        Region region = regionDao.selectOne(queryWrapper);
        if (region != null) {
            return regionDao.selectById(region.getParentId());
        }
        return null;
    }

    private List<Region> selectByRegionName(Region region) {
        QueryWrapper<Region> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("region", region.getRegion()).or().like("another_name", region.getRegion());
        List<Region> regions = regionDao.selectList(queryWrapper);
        return regions;
    }
}
